package zxing.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.Contract;
import io.virtualapp.bean.GetNewBean;
import io.virtualapp.bean.UpdateBean;
import io.virtualapp.mylibrary.callback.BaseCallback;
import io.virtualapp.mylibrary.util.HttpUtils;
import io.virtualapp.mylibrary.util.ToastUtils;
import io.virtualapp.mylibrary.util.Utils;
import io.virtualapp.widgets.DialogDownLoadAPK;
import okhttp3.Request;
import okhttp3.Response;

public class AboutActivity extends VActivity {
    @BindView(R.id.edition)
    TextView edition;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.app_name)
    TextView appName;
    @BindView(R.id.version_code)
    TextView versionCode;
    @BindView(R.id.rv_icon)
    RelativeLayout rvIcon;

    @BindView(R.id.lv_checkUpdate)
    RelativeLayout lvCheckUpdate;
    @BindView(R.id.txt_conract)
    TextView txtConract;
    Unbinder unbinder;
    private UpdateBean bean;
    Contract contractBean;
    @BindView(R.id.iv_dis)
    ImageView ivDis;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        initDatas();
    }

    public void initDatas() {
        update();
        ivDis.setOnClickListener(view -> finish());
        versionCode.setText("版本：" + getVersionCode());
        bean = DataSupport.findFirst(UpdateBean.class);
        contractBean = DataSupport.findFirst(Contract.class);
        //联系电话
        if (contractBean != null) {
            txtConract.setText("联系方式" + ":" + contractBean.getTxt() + "-" + contractBean.getNum());
        }
        lvCheckUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                update();
            }
        });
    }

    //获取版本号
    private String getVersionCode() {
        String localVersion = null;
        try {
            PackageInfo packageInfo = this.getApplicationContext().getPackageManager().getPackageInfo(this.getPackageName(), 0);
            localVersion = packageInfo.versionName;
            // LogUtil.d("TAG", "本软件的版本号。。" + localVersion);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    private void update() {
        if (Utils.isNetworkAvailable(getApplicationContext())) {
            HttpUtils.getInstance().postNews(new BaseCallback<GetNewBean>() {
                @Override
                public void onRequestBefore() {

                }

                @Override
                public void onFailure(Request request, Exception e) {

                }

                @Override
                public void onSuccess(Response response, GetNewBean o) {
                    if (response.isSuccessful()) {
                        if (o.isHasnew()) {
                            DialogDownLoadAPK loadAPK = new DialogDownLoadAPK(getActivity(), o.getVername() + "来啦");
                            loadAPK.show();
                            loadAPK.setOnClickListener(clicked -> {
                                if (clicked) {
                                    openActionView(o.getDownurl());
                                }
                            });
                        } else {
                            edition.setText("已是最新版");
                            ToastUtils.showShortToast("已经是最新版本");
                        }
                    }
                }

                @Override
                public void onError(Response response, int errorCode, Exception e) {

                }
            });
        } else {
            ToastUtils.showLongToast("请检查网络链接");
        }
    }

}
